package com.tanhua.manage.listener;

import com.tanhua.manage.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class RedisKeyListener extends KeyExpirationEventMessageListener {
    @Autowired
    private UserService userService;


    public RedisKeyListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey=message.toString();
        if(expiredKey.contains("Freeze_")){
            log.info("redis key 过期：{}",expiredKey);
            String[] split = expiredKey.split("_");
            String userId=split[2];
            HashMap<String, Object> param = new HashMap<>();
            param.put("userId",Integer.valueOf(userId));
            param.put("frozenRemarks","自动解冻");
            userService.unfreeze(param);
            log.info("redis中的键过期已完成解冻操作",expiredKey);


        }
    }
}
